package com.tql.mall.common.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @Author: tyk
 * @Date: 2023/4/15 19:23
 * @Description: 全局统一返回结果类
 */
@Data
@ApiModel(value = "全局统一返回结果")
public class R<T> {

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String msg;

    @ApiModelProperty(value = "返回数据")
    private T data;

    private R() {
    }

    private R(T data, RC rc) {
        this.code = rc.getCode();
        this.msg = rc.getMsg();
        this.data = data;
    }


    /**
     * 请求成功
     */
    public static <T> R<T> ok() {
        return ok(RC.SUCCESS);
    }

    /**
     * 请求成功
     */
    public static <T> R<T> ok(T data) {
        return ok(data, RC.SUCCESS);
    }

    /**
     * 请求成功
     */
    public static <T> R<T> ok(RC rc) {
        return new R<>(null, rc);
    }

    /**
     * 请求成功
     */
    public static <T> R<T> ok(T data, RC rc) {
        return new R<>(data, rc);
    }

    /**
     * 请求失败
     */
    public static <T> R<T> fail() {
        return fail(RC.FAIL);
    }


    /**
     * 请求失败
     */
    public static <T> R<T> fail(T data) {
        return fail(data, RC.FAIL);
    }

    /**
     * 请求失败
     */
    public static <T> R<T> fail(RC rc) {
        return fail(null, rc);
    }

    /**
     * 请求失败
     */
    public static <T> R<T> fail(T data, RC rc) {
        return new R<>(data, rc);
    }

    /**
     * 设置返回信息
     *
     * @param msg 返回信息
     */
    public R<T> msg(String msg) {
        this.setMsg(msg);
        return this;
    }


    public R<T> code(Integer code){
        this.setCode(code);
        return this;
    }
}
